import PureAuthDevTeams from '../components/auth-dev-teams';
import * as CompanyAction from 'actions/company';
import { getFetchStatus } from 'actions/util';
import * as React from 'react';
import * as ReactRedux from 'react-redux';

const mapStateToProps = (state) => {
  return {
    fetching: getFetchStatus(state, CompanyAction.listAuthedDevTeams),
    authFetching: getFetchStatus(state, CompanyAction.authTeam),
    devTeams: state.companyAuthedDevTeamList
  };
};
const mapDispatchToProps = (dispatch, ownProps) => {
  const { companyId } = ownProps;

  return {
    initFetch: () => {
      return dispatch(CompanyAction.listAuthedDevTeams(companyId));
    },
    onAuthDevTeam: (teamId) => {
      return dispatch(CompanyAction.authTeam(companyId, teamId))
        .then(({ error }) => {
          if (!!error) {
            return Promise.reject();
          }
          return dispatch(CompanyAction.listAuthedDevTeams(companyId));
        });
    },
    onUnAuthDevTeam: (teamId) => {
      return dispatch(CompanyAction.unAuthTeam(companyId, teamId));
    }
  };
};

class AuthDevTeams extends React.Component {
  componentDidMount() {
    this.props.initFetch();
  }

  render() {
    return <PureAuthDevTeams {...this.props}/>;
  }
}

export default ReactRedux.connect(mapStateToProps, mapDispatchToProps)(AuthDevTeams);
